package ch8;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TalkServer {
	public static void main(String[] args) {

		try {
			ServerSocket st = new ServerSocket(6666);
			System.out.println("服务端启用成功！");
			while (true) {
				Socket s = null;
				try {
					s = st.accept();// 接收
					System.out.println("本地：" + s.getLocalAddress() + "端口：" + s.getLocalPort());
					System.out.println("远程：" + s.getInetAddress() + ",端口：" + s.getPort());
					InputStream in = s.getInputStream();
					OutputStream out = s.getOutputStream();
					BufferedReader br = new BufferedReader(new InputStreamReader(in));
					PrintWriter pw = new PrintWriter(out);
					while (true) {
						String str2 = br.readLine();
						System.out.println("你说：" + str2);

						String str = "汪翔好几天没有来了！" + new java.util.Date();
						pw.println(str);
						pw.flush();
						if ("exit".equals(str2))
							break;
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if (s != null)
							s.close();
					} catch (Exception e) {
						// TODO 自动生成的 catch 块
						e.printStackTrace();
					}
				}
			}
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

	}
}
